package com.cjst.service.impl.sys;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cjst.dao.mapper.sys.SysMenuMapper;
import com.cjst.pojo.sys.SysMenu;
import com.cjst.pojo.sys.SysRelation;
import com.cjst.pojo.sys.SysRole;
import com.cjst.pojo.sys.SysUser;
import com.cjst.sdp.util.IsMenu;
import com.cjst.sdp.util.MenuNode;
import com.cjst.sdp.util.MenuStatus;
import com.cjst.sdp.util.ZTreeNode;
import com.cjst.service.impl.base.BaseServiceImpl;
import com.cjst.service.sys.SysMenuService;
import com.cjst.service.sys.SysRelationService;
import com.cjst.service.sys.SysRoleService;
import com.cjst.sql.C;
import com.cjst.sql.Method;
import com.cjst.sql.WherePrams;
import com.cjst.util.CloneClass;

import jdk.nashorn.internal.objects.annotations.Where;

/**
 * 菜单
 * @author yj
 *
 */
@Service
public class SysMenuServiceImpl extends BaseServiceImpl<SysMenu, String> implements SysMenuService{
	
	@Autowired
	private SysMenuMapper menuDao;
	
	@Autowired
	private SysRelationService relationService;
	

	@Autowired
	private SysRoleService roleService;
	
	@Override
	public List<MenuNode> menuRight(SysUser sysUser,Integer isNose){
		List<MenuNode> list=new ArrayList<MenuNode>();
		String roleId = sysUser.getRoleId();
		if(StringUtils.isNotEmpty(roleId)) {
			SysRole role = roleService.get(roleId);
			if(role!=null) {
				if("admin".equals(role.getName())) { //如果角色是超级管理员，就直接查询所有菜单权限
					WherePrams wherePrams=Method.where("status", C.EQ, "1");
					wherePrams.and("isNose", C.EQ, isNose);
					if(isNose==1){
						wherePrams.orderBy(" num asc ");
					}
					List<SysMenu> menuList=this.list(wherePrams);
					if(menuList!=null && menuList.size()>0){
						for (SysMenu sysMenu : menuList) {
							MenuNode menuNode=new MenuNode();
							CloneClass.getClone(sysMenu, menuNode);
							menuNode.setId(sysMenu.getId());
							list.add(menuNode);
						}
					}
				}else { 
					//1.非超级管理员，就根据角色查询对应的权限
					WherePrams where =Method.where("roleid", C.EQ, roleId);

					List<SysRelation> relations = relationService.list(where);
					
					//2.根据权限，查询用户菜单
					List<String> menuIds = new ArrayList<>();
					menuIds.add("");
					for (SysRelation sysRelation : relations) {
						menuIds.add(sysRelation.getMenuid());
					}
					WherePrams menuPrams = Method.where("status", C.EQ, "1");
					menuPrams.and("isNose", C.EQ, isNose);
					menuPrams.andIn("id", C.IN, menuIds);
					if(isNose==1){
						menuPrams.orderBy(" num asc ");
					}
					List<SysMenu> menus = this.list(menuPrams);
					if(menus!=null&&menus.size()>0) {
						for (SysMenu sysMenu : menus) {
							MenuNode menuNode=new MenuNode();
							CloneClass.getClone(sysMenu, menuNode);
							menuNode.setId(sysMenu.getId());
							list.add(menuNode);
						}
					}
						
				}
					
				
			}
		}
		
		
		/*List<SysMenu> menuList=this.list(WherePrams);
		if(menuList!=null && menuList.size()>0){
			for (SysMenu sysMenu : menuList) {
				MenuNode menuNode=new MenuNode();
				CloneClass.getClone(sysMenu, menuNode);
				menuNode.setId(sysMenu.getId());
				list.add(menuNode);
			}
		}*/
		return list;
	}
	
	
	@Override
	public List<ZTreeNode> tree(){
		WherePrams wherePrams=Method.where("status", C.EQ, "1");
		List<SysMenu> sysMenuList=this.list(wherePrams);
		List<ZTreeNode> nodeList=new ArrayList<ZTreeNode>();
		if(sysMenuList!=null && sysMenuList.size()>0){
			for (SysMenu sysMenu : sysMenuList) {
				ZTreeNode node=new ZTreeNode();
				CloneClass.getClone(sysMenu, node);
				node.setId(sysMenu.getId());
				node.setpId(sysMenu.getParentId());
				node.setName(sysMenu.getName());
				nodeList.add(node);
			}
		}
		return nodeList;
	}

	@Override
	public List<SysMenu> selectMenus(String condition, String level) {
		SysMenu menu = new SysMenu();
		menu.setCode(condition);
		menu.setName(condition);
		if(StringUtils.isNotEmpty(level)){
			menu.setLevels(Integer.parseInt(level));
		}		
		return menuDao.selectMenus(menu);
	}

	/**
	 * 由于bootstraptreetable不支持formatter函数，bean又不能随便添加属性，所以用map接收返回值
	 * xfc
	 */
	@Override
	public List<Map<String, Object>> getMenuMap(String condition, String level) {
		SysMenu menu = new SysMenu();
		menu.setCode(condition);
		menu.setName(condition);
		if(StringUtils.isNotEmpty(level)){
			menu.setLevels(Integer.parseInt(level));
		}
		List<Map<String, Object>> menus = menuDao.getMenuMap(menu);
		for (Map<String, Object> map : menus) {
			Integer ismenu = (Integer)map.get("ismenu");			
			map.put("isMenuName", IsMenu.valueOf(ismenu));			
			String status = (String)map.get("status");
			if(StringUtils.isNotEmpty(status)){
				map.put("statusName", MenuStatus.valueOf(Integer.parseInt(status)));
			}
		}
		return menus;
	}




	
	
}
